<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" class="data-form">
      <el-form-item>
        <el-input v-model="dataForm.keywords" placeholder="会员账号" clearable></el-input>
      </el-form-item>
      <el-form-item class="page-form-item">
      <el-select v-model="dataForm.province" placeholder="请选择省" @change="provinceChanged" clearable>
        <el-option
          v-for="item in this.provinceList"
          :key="item.id"
          :label="item.areaName"
          :value="item.id">
        </el-option>
      </el-select>
      </el-form-item>
      <el-form-item class="page-form-item">
      <el-select v-model="dataForm.city" placeholder="请选择市" @change="provinceChanged1" v-if="this.cityType == 1" clearable>
        <el-option
          v-for="item in this.cityList"
          :key="item.id"
          :label="item.areaName"
          :value="item.id">
        </el-option>
      </el-select>
      </el-form-item>
      <el-form-item class="page-form-item">
      <el-select v-model="dataForm.area" placeholder="请选择区" v-if="this.areaType == 1" clearable>
        <el-option
          v-for="item in this.areaList"
          :key="item.id"
          :label="item.areaName"
          :value="item.id">
        </el-option>
      </el-select>
      </el-form-item>
      <!-- <el-form-item class="page-form-item">
      <el-select v-model="dataForm.town" placeholder="请选择镇" v-if="this.townType == 1">
        <el-option
          v-for="item in this.townList"
          :key="item.id"
          :label="item.areaName"
          :value="item.id">
        </el-option>
      </el-select>
      </el-form-item> -->
      <el-form-item label="起始时间">
				<el-date-picker
          v-model="dataForm.startTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="起始时间">
			  </el-date-picker>
			</el-form-item>
			<el-form-item label="结束时间">
				<el-date-picker
          v-model="dataForm.endTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="结束时间">
			  </el-date-picker>
			</el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button icon="el-icon-download" type="primary" @click="excel()">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="统计信息">
        <template slot-scope="scope">
          <span>{{userType[dataList[scope.$index].userType - 1]}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="dataList"
        header-align="center"
        align="center"
        label="数量">
        <template slot-scope="scope">
          <span>{{dataList[scope.$index].count}}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="120"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('statistics:queryUser')" type="text" size="small" @click="upgradeOpen(dataList[scope.$index].userType)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination> -->
    <!-- 弹窗, 新增 / 修改 -->
    <!-- <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update> -->
    <el-dialog
      title="用户列表"
      :visible.sync="addOrUpdateVisible"
      width="80%">
      <upgrade ref="upgrade" @refreshDataList="getDataList"></upgrade>
    </el-dialog>
  </div>
</template>

<script>
import {userType} from '@/utils/userType'
import Upgrade from './userstatistics'
export default {
  data () {
    return {
      dataForm: {
        userType: '',
        userId: '',
        city: '',
        area: '',
        province: '',
        startTime: '',
        endTime: '',
        keywords: ''
      },
      provinceList: [],
      cityList: [],
      townList: [],
      areaList: [],
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      extractMoneyDetailsVisible: false,
      merchantIncomeVisible: false,
      cityType: 0,
      areaType: 0,
      userType: userType
    }
  },
  components: {
    Upgrade
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/member/area/queryProvinceList'),
        method: 'post'
      }).then(({data}) => {
        this.provinceList = data.data
      })
      this.$http({
        url: this.$http.adornUrl('/statistics/queryMembership'),
        method: 'post',
        params: this.$http.adornParams(
          {
            'startTime': this.dataForm.startTime,
            'endTime': this.dataForm.endTime,
            'province': this.dataForm.province,
            'city': this.dataForm.city,
            'area': this.dataForm.area,
            'keywords': this.dataForm.keywords
          }
        )
      }).then(({data}) => {
        if (data && data.code === 0) {
          debugger
          this.dataList = data.data
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    // sizeChangeHandle (val) {
    //   this.pageSize = val
    //   this.pageIndex = 1
    //   this.getDataList()
    // },
    // 当前页
    // currentChangeHandle (val) {
    //   this.pageIndex = val
    //   this.getDataList()
    // },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    // addOrUpdateHandle (id) {
    //   this.addOrUpdateVisible = true
    //   this.$nextTick(() => {
    //     this.$refs.addOrUpdate.init(id)
    //   })
    // },
    upgradeOpen (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.upgrade.getDataList(this.dataForm.province, this.dataForm.city, this.dataForm.area, this.dataForm.startTime, this.dataForm.endTime, id, this.dataForm.keywords)
      })
    },
    provinceChanged (id) {
      this.$http({
        url: this.$http.adornUrl('/member/area/querySubordinateList'),
        method: 'post',
        data: this.$http.adornData(id, false)
      }).then(({data}) => {
        this.cityList = data.data
        this.cityType = 1
        this.areaType = 0
        this.dataForm.city = ''
      })
    },
    provinceChanged1 (id) {
      this.$http({
        url: this.$http.adornUrl('/member/area/querySubordinateList'),
        method: 'post',
        data: this.$http.adornData(id, false)
      }).then(({data}) => {
        this.areaList = data.data
        this.areaType = 1
        this.dataForm.area = ''
      })
    },
    superUserOpen (val) {
      console.log(val)
      this.$http({
        url: this.$http.adornUrl('/member/user/openNewPlayer'),
        method: 'post',
        data: this.$http.adornData(val, false)
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    excel () {
      window.location.href = this.$http.adornUrl(`/statistics/exportMembershipLowerExcel?token=${this.$cookie.get('token')}&keywords=${this.dataForm.keywords}`)
    }
  }
}
</script>
